<?php
class Ext_View_Helper_ExtNavigation extends Zend_View_Helper_Abstract
{
    const CURRENT_MENU_ITEM_CLASS = "current_menu_item";

    private $_navModel;

    public function extNavigation( $class = null )
    {
        $this->_navModel = new Ext_Model_Navigation();
        $items = $this->_navModel->fetchAll(
            $this->_navModel->select()
            ->where('parent_id = 0' )
            ->where( " type='admin' ")
            ->order('priority')
        );
        if ( !empty( $class ) ) {
            echo '<ul id="topnav" class="' . $class . '">';
        } else {
            echo '<ul id="topnav">';
        }
        foreach( $items as $item ) {
            $toEcho = "";
            if( $item->secure == 'T') {
                $url = $this->view->surl( $item->controller, $item->action, array(), "%s", $item->module, true );
                if ( ! empty( $url ) ) {
                    $toEcho .= $this->_getLink( $item, $url );
                }
            } else {
                $url = $this->view->url( array(
                    'controller' => $item->controller,
                    'action' => $item->action,
                    'module' => $item->module
                    )
                );
                if( $item->auth == 'T'){
                    $auth = Zend_Auth::getInstance();
                    if ( $auth->hasIdentity() ) {
                        $toEcho .=  $this->_getLink( $item, $url );
                    } else {
                        continue;
                    }
                } else {
                    $toEcho .=  $this->_getLink( $item, $url );
                }
            }
            $toEcho .= $this->fillSubMenu( $item );
            if ( !empty( $toEcho ) ) {
                echo "<li>$toEcho</li>";
            }
        }
        echo "</ul>";
    }

    public function _getLink( $item, $url )
    {
        $front = Zend_Controller_Front::getInstance();
        $request = $front->getRequest();

        $class = $item->html_class;
        if ( empty( $item->module ) || $request->getModuleName() == $item->module ) {
            if ( empty( $item->controller ) || $request->getControllerName() == $item->controller ) {
                if ( $request->getActionName() == $item->action ) {
                    $class .=" " . self::CURRENT_MENU_ITEM_CLASS;
                }
            }
        }

        $ret = '<a href="'. $url . '"';
        if ( !empty( $class ) ) {
            $ret .= ' class="' . $class .'"';
        }
        if ( !empty( $item->html_id ) ) {
            $ret .= ' id="' . $item->html_id .'"';
        }
        $ret .= ">" . $item->title. "</a>";
        return $ret;
    }

    public function fillSubMenu( $item )
    {
        $ret = "";
        $children = $this->_navModel->fetchChildren( $item );
        if( count($children) ) {
            $ret.= "<span>";
            foreach( $children as $child ) {
                $link = $this->view->href( $child->controller, $child->action, $child->title, array(), "%s", $child->module );
                if( !is_null ( $link )) {
                    $ret .= $link . " | ";
                }
            }
            $ret .= "</span>";
        }
        return $ret;
    }
}
